﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ui="clr-namespace:Bonobo.Irc.Client.UI"
    xmlns:data="clr-namespace:Bonobo.Irc.Client.UI.Data"    
    xmlns:globalization="clr-namespace:System.Globalization;assembly=mscorlib"    
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"            
    x:Class="Bonobo.Irc.Client.UI.Data.IrcChannelConversationTemplate">

    <DataTemplate x:Key="IrcChannelConversationTemplate" >
        <Grid FocusManager.FocusedElement="{Binding ElementName=txtMessage}"> 
            <Grid.Resources>
                <CollectionViewSource Source="{Binding People}" x:Key="SortedPeople">
                    <CollectionViewSource.SortDescriptions>
                        <scm:SortDescription PropertyName="IsOperator" Direction="Descending" />
                        <scm:SortDescription PropertyName="Name"/>
                    </CollectionViewSource.SortDescriptions>
                </CollectionViewSource>
                <data:MessageConverter x:Key="MessageConverter" />
                <data:MessageTypeToBrushConverter x:Key="MessageTypeToBrushConverter" />
                <data:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
            </Grid.Resources>
            <Grid.CommandBindings>
                <CommandBinding Command="{x:Static ui:IrcCommands.List}" 
                                Executed="ListExecuted" />
                <CommandBinding Command="{x:Static ui:IrcCommands.Chat}" 
                                Executed="ChatExecuted" />
                <CommandBinding Command="{x:Static ui:IrcCommands.Send}" 
                                Executed="SendExecuted" />
            </Grid.CommandBindings>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition Width="100px" />
                <ColumnDefinition Width="200px" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="27px" />
                <RowDefinition />
                <RowDefinition Height="27px" />
                <RowDefinition Height="27px" />
            </Grid.RowDefinitions>
            
            <ListBox Name="lvItems"
                     BorderThickness="0"
                     ItemsSource="{Binding Messages}"
                     SelectionMode="Single"
                     ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                     Grid.Row="0"
                     Grid.RowSpan="3"
                     Grid.Column="0"
                     Grid.ColumnSpan="2"
                     data:SelectorExtenders.IsAutoscroll="true" 
                     IsSynchronizedWithCurrentItem="True"
                     Padding="5">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <DockPanel>
                            <TextBlock Text="{Binding TimeStamp, 
                                                StringFormat={}{0:g},
                                                ConverterCulture={x:Static globalization:CultureInfo.CurrentCulture}}"
                                           Foreground="Gray"
                                           TextWrapping="Wrap"
                                           Margin="0 2px 10px 2px" />
                            <TextBlock TextWrapping="Wrap"
                                           DockPanel.Dock="Right"
                                           Margin="0 2px 0 2px"
                                           Foreground="{Binding Type, Converter={StaticResource MessageTypeToBrushConverter}}"
                                           Text="{Binding Converter={StaticResource MessageConverter}}">                                    
                             </TextBlock>
                        </DockPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
              
            <TextBox Margin="2"
                     HorizontalAlignment="Stretch"
                     Grid.Row="3"
                     Grid.Column="0"
                     MaxLength="450"
                     Name="txtMessage">                        
            </TextBox>
            <Button Margin="2px"
                    Grid.Row="3"
                    Grid.Column="1"
                    Command="{x:Static ui:IrcCommands.Send}"
                    CommandParameter="{Binding}"
                    IsDefault="True">
                _Send
            </Button>
            

            <TextBlock Grid.Column="2"
                       Grid.Row="0"
                       Padding="2"
                       Foreground="Gray"
                       FontWeight="Bold"
                       Background="White">
                    People
            </TextBlock>
            <ListBox Grid.Column="2"
                     Grid.Row="1"
                     Name="lvPeople"
                     SelectionMode="Multiple"
                     BorderThickness="0"
                     ItemsSource="{Binding Source={StaticResource SortedPeople }}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <WrapPanel>
                            <TextBlock Text="@"  Visibility="{Binding IsOperator, Converter={StaticResource BoolToVisibilityConverter}}" />                            
                            <TextBlock Text="{Binding Name }"
                                           />
                        </WrapPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button Grid.Column="2"
                    Grid.Row="2"
                    Margin="2"
                    Command="{x:Static ui:IrcCommands.List}"
                    CommandParameter="{Binding}">_Refresh</Button>
            
            <Button Grid.Column="2"
                    Grid.Row="3"
                    Margin="2"
                    Command="{x:Static ui:IrcCommands.Chat}"
                    CommandParameter="{Binding}">_Private Chat</Button>
        </Grid>
    </DataTemplate>
</ResourceDictionary>